﻿using System;
using System.Windows.Input;
using Cirrious.MvvmCross.Commands;
using TeaLightV5.Foundation.BL;
using TeaLightV5.Foundation.Sal.Interfaces;
using TeaLightV5.Foundation.UX.Interfaces.Models;

namespace TeaLightV5.Foundation.UX.ViewModels
{
    public abstract class CreateEntityViewModel<T, TE, TKey>
        : UpdateEntityViewModel<T, TE, TKey>
        where TE : class, IEntity<TKey>, new()
        where T : IEntityModel<TE, TKey>, new()
         
    {
        protected CreateEntityViewModel()
            : base(null)
        {            
        }

        protected CreateEntityViewModel(ModelParameters<TKey> parameters)
            : base(parameters)
        {
        }

        public override ICommand SaveCommand
        {
            get
            {
                return new MvxRelayCommand(
                    () =>
                    {
                        try
                        {
                            ClearException();
                            AddNew();
                            RequestClose(this);
                        }
                        catch (Exception exception)
                        {
                            HandleException(exception);
                        }
                    });
            }
        }
    }
}
